package com.example.smart.adapter.home

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.marginTop
import com.example.lib.extend.setOnDoubleClickListener
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.data.CommandData
import com.example.smart.databinding.ItemCommandBinding
import com.example.smart.dialog.CommandDialog

class CommandAdapter(val activity: Activity) :
    BaseRecyclerAdapter<ItemCommandBinding, CommandData>(activity, mutableListOf()) {
    var onHeight: ((Int) -> Unit)? = null
    private val mMap = HashMap<Int, Boolean>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemCommandBinding> {
        return BaseViewHolder(ItemCommandBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_command, parent, false)))
    }

    override fun onBindView(itemView: ItemCommandBinding, bean: CommandData, position: Int) {
        itemView.root.post {
            onHeight?.invoke(itemView.root.height + itemView.root.marginTop)
            onHeight = null
        }

        itemView.ItemImage.setImageResource(bean.photo)
        itemView.ItemName.text = bean.name
        itemView.ItemTime.text = bean.time
        itemView.ItemMsg.text = bean.msg
        itemView.CommandText2.text = bean.command.toString()
        itemView.LikeText2.text = bean.like.toString()

        if (mMap[position] == true) {
            itemView.LikeImage2.setImageResource(R.mipmap.icon_like2_on)
        } else {
            itemView.LikeImage2.setImageResource(R.mipmap.icon_like2)
        }

        itemView.LikeLayout.setOnDoubleClickListener {
            if (mMap[position] == null || !mMap[position]!!) {
                updateItem(position, getItemAtPosition(position).also {
                    it.like++
                }, "update")
                mMap[position] = true
            } else {
                updateItem(position, getItemAtPosition(position).also {
                    it.like--
                }, "update")
                mMap[position] = false
            }
        }
    }

    override fun onItemChangeBindView(itemView: ItemCommandBinding,
        bean: CommandData,
        position: Int,
        payloads: MutableList<Any>) {
        itemView.LikeText2.text = bean.like.toString()

        if (mMap[position] == true) {
            itemView.LikeImage2.setImageResource(R.mipmap.icon_like2_on)
        } else {
            itemView.LikeImage2.setImageResource(R.mipmap.icon_like2)
        }

        itemView.LikeLayout.setOnDoubleClickListener {
            if (mMap[position] == null || !mMap[position]!!) {
                updateItem(position, getItemAtPosition(position).also {
                    it.like++
                }, "update")
                mMap[position] = true
            } else {
                updateItem(position, getItemAtPosition(position).also {
                    it.like--
                }, "update")
                mMap[position] = false
            }
        }
    }
}